package com.sinbader.xiaole.common.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import com.sinbader.xiaole.common.entity.CheckStockItem;
import com.sinbader.xiaole.common.mapper.CheckStockItemMapper;
import com.sinbader.xiaole.common.model.params.CheckStockItemParam;
import com.sinbader.xiaole.common.model.result.CheckStockItemResult;
import  com.sinbader.xiaole.common.service.CheckStockItemService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * 盘点项 服务实现类
 */
@Service
public class CheckStockItemServiceImpl extends ServiceImpl<CheckStockItemMapper, CheckStockItem> implements CheckStockItemService {

    @Override
    public void add(CheckStockItemParam param){
        CheckStockItem entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(CheckStockItemParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(CheckStockItemParam param){
        CheckStockItem oldEntity = getOldEntity(param);
        CheckStockItem newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public CheckStockItemResult findBySpec(CheckStockItemParam param){
        return null;
    }

    @Override
    public List<CheckStockItemResult> findListBySpec(CheckStockItemParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(CheckStockItemParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(CheckStockItemParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private CheckStockItem getOldEntity(CheckStockItemParam param) {
        return this.getById(getKey(param));
    }

    private CheckStockItem getEntity(CheckStockItemParam param) {
        CheckStockItem entity = new CheckStockItem();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
